package com.jprocms.module.cms.dal.mysql.voteitem;

import com.jpro.framework.common.pojo.PageResult;
import com.jpro.framework.mybatis.core.mapper.BaseMapperX;
import com.jpro.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.jprocms.module.cms.controller.admin.voteitem.vo.VoteItemListReqVO;
import com.jprocms.module.cms.controller.admin.voteitem.vo.VoteItemPageReqVO;
import com.jprocms.module.cms.dal.dataobject.voteitem.VoteItemDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.lang.Nullable;

import java.util.List;

/**
 * 投票项 Mapper
 *
 * @author jprocms
 */
@Mapper
public interface VoteItemMapper extends BaseMapperX<VoteItemDO> {
    /**
     * 根据主键获取数据
     *
     * @param id 主键ID
     * @return 实体对象。没有找到数据，则返回 {@code null}
     */
    @Nullable
    VoteItemDO select(Integer id);

    /**
     * 根据投票ID获取列表
     *
     * @param voteId 投票ID
     * @return 数据列表
     */
    List<VoteItemDO> selectByVoteId(Long voteId);

    /**
     * 投票
     *
     * @param voteId  投票ID
     * @param itemIds 选项ID列表
     * @return 更新条数
     */
    int vote(@Param("voteId") Long voteId, @Param("itemIds") List<Long> itemIds);

    /**
     * 根据投票ID删除数据
     *
     * @param voteId 投票ID
     * @param ids    不包含的id
     * @return 删除条数
     */
    int deleteByVoteId(@Param("voteId") Long voteId, @Param("ids") List<Long> ids);

    /**
     * 根据站点ID删除数据
     *
     * @param siteId 站点ID
     * @return 被删除的数据条数
     */
    int deleteBySiteId(@Param("siteId") Long siteId);

    default PageResult<VoteItemDO> selectPage(VoteItemPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<VoteItemDO>()
                .eqIfPresent(VoteItemDO::getVoteId, reqVO.getVoteId())
                .eqIfPresent(VoteItemDO::getTitle, reqVO.getTitle())
                .eqIfPresent(VoteItemDO::getCount, reqVO.getCount())
                .eqIfPresent(VoteItemDO::getItemSort, reqVO.getItemSort())
                .orderByDesc(VoteItemDO::getId));
    }

    default List<VoteItemDO> selectList(VoteItemListReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<VoteItemDO>()
                .eqIfPresent(VoteItemDO::getVoteId, reqVO.getVoteId())
                .eqIfPresent(VoteItemDO::getTitle, reqVO.getTitle())
                .eqIfPresent(VoteItemDO::getCount, reqVO.getCount())
                .eqIfPresent(VoteItemDO::getItemSort, reqVO.getItemSort())
                .orderByDesc(VoteItemDO::getId));
    }

}
